package com.ruoyi.psi.inventory.transfer.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDomain;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDto;
import com.ruoyi.psi.base.warehouse.mapper.WarehouseMapper;
import com.ruoyi.psi.inventory.transfer.domain.InventoryTransfer;
import com.ruoyi.psi.inventory.transfer.domain.InventoryTransferList;
import com.ruoyi.psi.inventory.transfer.mapper.InventoryTransferMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 产品库存调拨
 * 业务执行层
 */
@Service
public class InventoryTransferServiceImpl implements IInventoryTransferService {
    @Autowired
    private InventoryTransferMapper inventoryTransferMapper;
    @Autowired
    private WarehouseMapper warehouseMapper; //仓库
    @Autowired
    private IdUtil idUtil;

    /**
     * 仓库调拨查询
     *
     * @param inventoryTransfer
     * @return
     */
    @Override
    public List<InventoryTransfer> selectInventoryTransferList(InventoryTransfer inventoryTransfer) {
        List<InventoryTransfer> inventoryTransfers = inventoryTransferMapper.selectInventoryTransferList(inventoryTransfer);
        for (int i = 0; i < inventoryTransfers.size(); i++) {
            if (StringUtils.isNotNull(inventoryTransferMapper.selectInventoryTransferListListById(inventoryTransfers.get(i).getInventoryTransferId()))) {
                //存在，则存出仓库调拨对应产品信息
                inventoryTransfers.get(i).setInventoryTransferLists(inventoryTransferMapper.selectInventoryTransferListListById(inventoryTransfers.get(i).getInventoryTransferId()));
            }
        }
        return inventoryTransfers;
    }

    /**
     * 按编号查询
     *
     * @param inventoryTransferId 仓库调拨编号
     * @return
     */
    @Override
    public InventoryTransfer selectInventoryTransferById(String inventoryTransferId) {
        return inventoryTransferMapper.selectInventoryTransferById(inventoryTransferId);
    }

    /**
     * 仓库调拨新增
     *
     * @param inventoryTransfer
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertInventoryTransfer(InventoryTransfer inventoryTransfer) throws Exception {
        String inventoryTransferId = idUtil.createInventoryTransferId();//自动生成编号
        if (StringUtils.isNotNull(inventoryTransferMapper.selectInventoryTransferById(inventoryTransferId))) {
            //存在
            return 0;
        }
        //不存在
        inventoryTransfer.setInventoryTransferId(inventoryTransferId);//主表添加编号
        inventoryTransferMapper.insertInventoryTransfer(inventoryTransfer);//仓库调拨据
        List<WarehouseListDto> warehouseListDtosOut = warehouseMapper.selectWarehouseListListById(inventoryTransfer.getWarehouseOutId()); // 查询转出仓库产品列表
        List<WarehouseListDto> warehouseListDtosIn = warehouseMapper.selectWarehouseListListById(inventoryTransfer.getWarehouseInId()); // 查询转入仓库产品列表
        List<InventoryTransferList> inventoryTransferLists = inventoryTransfer.getInventoryTransferLists();//仓库调拨产品信息
        for (int i = 0; i < inventoryTransferLists.size(); i++) {
            inventoryTransferLists.get(i).setInventoryTransferId(inventoryTransferId);//子表添加编号
            // 操作转出仓库库存数量
            WarehouseListDomain warehouseListOut = new WarehouseListDomain(); // 转出仓库子表的对象
            warehouseListOut.setWarehouseId(inventoryTransfer.getWarehouseOutId());
            warehouseListOut.setProduceId(inventoryTransferLists.get(i).getProduceId());
            // 遍历调出仓库
            for (int j = 0; j < warehouseListDtosOut.size(); j++) {
                if (inventoryTransferLists.get(i).getProduceId().equals(warehouseListDtosOut.get(j).getProduceId())) {
                    // 该产品存在，更新产品数量，产品数量 = 原库存数 - 该产品转移的数量
                    warehouseListOut.setProductNumber(warehouseListDtosOut.get(j).getProductNumber() - inventoryTransferLists.get(i).getInventoryTransferNumber());
                    break; //终止循环
                }
            }
            warehouseMapper.updateWarehouseList(warehouseListOut); //更新转出仓库库存数量
            // 操作转入仓库库存数量
            WarehouseListDomain warehouseListIn = new WarehouseListDomain(); // 转入仓库子表的对象
            warehouseListIn.setWarehouseId(inventoryTransfer.getWarehouseInId());
            warehouseListIn.setProduceId(inventoryTransferLists.get(i).getProduceId());
            boolean isExistFlag = false; //转入仓库是否存在该商品的标志，默认不存在
            // 遍历转入仓库
            for (int j = 0; j < warehouseListDtosIn.size(); j++) {
                if (inventoryTransferLists.get(i).getProduceId().equals(warehouseListDtosIn.get(j).getProduceId())) {
                    // 该产品存在，更新产品数量，产品数量 = 原库存数 + 该产品转移的数量
                    warehouseListIn.setProductNumber(warehouseListDtosIn.get(j).getProductNumber() + inventoryTransferLists.get(i).getInventoryTransferNumber());
                    isExistFlag = true;
                    break; //终止循环
                }
            }
            if (isExistFlag) {
                // 该产品存在，更新库存
                warehouseMapper.updateWarehouseList(warehouseListIn);
            } else {
                // 该产品不存在，在库存中新增产品
                warehouseListIn.setProductNumber(inventoryTransferLists.get(i).getInventoryTransferNumber());
                warehouseMapper.insertWarehouseList(warehouseListIn);
            }
            inventoryTransferMapper.insertInventoryTransferList(inventoryTransferLists.get(i));//仓库调拨产品信息
        }
        return 1;

    }

    /**
     * 仓库调拨更新
     *
     * @param inventoryTransfer
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateInventoryTransfer(InventoryTransfer inventoryTransfer) throws Exception {
        if (!StringUtils.isNotNull(inventoryTransferMapper.selectInventoryTransferById(inventoryTransfer.getInventoryTransferId()))) {
            //不存在
            return 0;
        }
        //存在仓库调拨单据
        inventoryTransferMapper.updateInventoryTransfer(inventoryTransfer);//仓库调拨单据
        List<InventoryTransferList> inventoryTransferListOld = inventoryTransferMapper.selectInventoryTransferListListById(inventoryTransfer.getInventoryTransferId()); //原子标记录
        List<InventoryTransferList> inventoryTransferListNew = inventoryTransfer.getInventoryTransferLists();// 新传入的子表信息
        List<WarehouseListDto> warehouseListDtosOut = warehouseMapper.selectWarehouseListListById(inventoryTransfer.getWarehouseOutId()); // 查询转出仓库产品列表
        List<WarehouseListDto> warehouseListDtosIn = warehouseMapper.selectWarehouseListListById(inventoryTransfer.getWarehouseInId()); // 查询转入仓库产品列表
        for (int i = 0; i < inventoryTransferListNew.size(); i++) {
            InventoryTransferList inventoryTransferList = inventoryTransferMapper.selectInventoryTransferListById(inventoryTransfer.getInventoryTransferId(), inventoryTransferListNew.get(i).getProduceId());
            if (StringUtils.isNotNull(inventoryTransferList)) {
                for (int j = 0; j < inventoryTransferListOld.size(); j++) {
                    if (inventoryTransferListOld.get(j).getProduceId().equals(inventoryTransferList.getProduceId())) {
                        inventoryTransferListOld.remove(j);
                        break;
                    }
                }
                //存在仓库调拨详细
                // 操作转出仓库库存数量
                WarehouseListDomain warehouseListOut = new WarehouseListDomain(); // 转出仓库子表的对象
                warehouseListOut.setWarehouseId(inventoryTransfer.getWarehouseOutId());
                warehouseListOut.setProduceId(inventoryTransferListNew.get(i).getProduceId());
                // 遍历调出仓库
                for (int j = 0; j < warehouseListDtosOut.size(); j++) {
                    if (inventoryTransferListNew.get(i).getProduceId().equals(warehouseListDtosOut.get(j).getProduceId())) {
                        // 该产品存在，更新产品数量，产品数量 = 原库存数 + 该产品原出库的数量 - 该产品新修改转移的数量
                        warehouseListOut.setProductNumber(warehouseListDtosOut.get(j).getProductNumber() + inventoryTransferList.getInventoryTransferNumber() - inventoryTransferListNew.get(i).getInventoryTransferNumber());
                        break; //终止循环
                    }
                }
                warehouseMapper.updateWarehouseList(warehouseListOut); //更新转出仓库库存数量
                // 操作转入仓库库存数量
                WarehouseListDomain warehouseListIn = new WarehouseListDomain(); // 转入仓库子表的对象
                warehouseListIn.setWarehouseId(inventoryTransfer.getWarehouseInId());
                warehouseListIn.setProduceId(inventoryTransferListNew.get(i).getProduceId());
                // 遍历转入仓库
                for (int j = 0; j < warehouseListDtosIn.size(); j++) {
                    if (inventoryTransferListNew.get(i).getProduceId().equals(warehouseListDtosIn.get(j).getProduceId())) {
                        // 该产品存在，更新产品数量，产品数量 = 原库存数 - 该产品原入库数量 + 该产品新修改i转移的数量
                        warehouseListIn.setProductNumber(warehouseListDtosIn.get(j).getProductNumber() - inventoryTransferList.getInventoryTransferNumber() + inventoryTransferListNew.get(i).getInventoryTransferNumber());
                        break; //终止循环
                    }
                }
                warehouseMapper.updateWarehouseList(warehouseListIn); //更新转入仓库库存数量
                //更新库存调拨出库的子表
                inventoryTransferMapper.updateInventoryTransferList(inventoryTransferListNew.get(i));
            } else {
                // 操作转出仓库库存数量
                WarehouseListDomain warehouseListOut = new WarehouseListDomain(); // 转出仓库子表的对象
                warehouseListOut.setWarehouseId(inventoryTransfer.getWarehouseOutId());
                warehouseListOut.setProduceId(inventoryTransferListNew.get(i).getProduceId());
                // 遍历调出仓库
                for (int j = 0; j < warehouseListDtosOut.size(); j++) {
                    if (inventoryTransferListNew.get(i).getProduceId().equals(warehouseListDtosOut.get(j).getProduceId())) {
                        // 该产品存在，更新产品数量，产品数量 = 原库存数 - 该产品转移的数量
                        warehouseListOut.setProductNumber(warehouseListDtosOut.get(j).getProductNumber() - inventoryTransferListNew.get(i).getInventoryTransferNumber());
                        break; //终止循环
                    }
                }
                warehouseMapper.updateWarehouseList(warehouseListOut); //更新转出仓库库存数量
                // 操作转入仓库库存数量
                WarehouseListDomain warehouseListIn = new WarehouseListDomain(); // 转入仓库子表的对象
                warehouseListIn.setWarehouseId(inventoryTransfer.getWarehouseInId());
                warehouseListIn.setProduceId(inventoryTransferListNew.get(i).getProduceId());
                boolean isExistFlag = false; //转入仓库是否存在该商品的标志，默认不存在
                // 遍历转入仓库
                for (int j = 0; j < warehouseListDtosIn.size(); j++) {
                    if (inventoryTransferListNew.get(i).getProduceId().equals(warehouseListDtosIn.get(j).getProduceId())) {
                        // 该产品存在，更新产品数量，产品数量 = 原库存数 + 该产品转移的数量
                        warehouseListIn.setProductNumber(warehouseListDtosIn.get(j).getProductNumber() + inventoryTransferListNew.get(i).getInventoryTransferNumber());
                        isExistFlag = true;
                        break; //终止循环
                    }
                }
                if (isExistFlag) {
                    // 该产品存在，更新库存
                    warehouseMapper.updateWarehouseList(warehouseListIn);
                } else {
                    // 该产品不存在，在库存中新增产品
                    warehouseListIn.setProductNumber(inventoryTransferListNew.get(i).getInventoryTransferNumber());
                    warehouseMapper.insertWarehouseList(warehouseListIn);
                }
                //新增库存调拨出库的子表
                inventoryTransferMapper.insertInventoryTransferList(inventoryTransferListNew.get(i));
            }
        }
        // 说明有删除的对象
        for (int i = 0; i < inventoryTransferListOld.size(); i++) {
            // 操作转出仓库库存数量
            WarehouseListDomain warehouseListOut = new WarehouseListDomain(); // 转出仓库子表的对象
            warehouseListOut.setWarehouseId(inventoryTransfer.getWarehouseOutId());
            warehouseListOut.setProduceId(inventoryTransferListOld.get(i).getProduceId());
            // 遍历调出仓库
            for (int j = 0; j < warehouseListDtosOut.size(); j++) {
                if (inventoryTransferListOld.get(i).getProduceId().equals(warehouseListDtosOut.get(j).getProduceId())) {
                    // 该产品存在，更新产品数量，产品数量 = 原库存数 + 该产品转移的数量
                    warehouseListOut.setProductNumber(warehouseListDtosOut.get(j).getProductNumber() + inventoryTransferListOld.get(i).getInventoryTransferNumber());
                    break; //终止循环

                }
            }
            warehouseMapper.updateWarehouseList(warehouseListOut); //更新转出仓库库存数量

            // 操作转入仓库库存数量
            WarehouseListDomain warehouseListIn = new WarehouseListDomain(); // 转入仓库子表的对象
            warehouseListIn.setWarehouseId(inventoryTransfer.getWarehouseInId());
            warehouseListIn.setProduceId(inventoryTransferListOld.get(i).getProduceId());
            // 遍历转入仓库
            for (int j = 0; j < warehouseListDtosIn.size(); j++) {
                if (inventoryTransferListOld.get(i).getProduceId().equals(warehouseListDtosIn.get(j).getProduceId())) {
                    // 该产品存在，更新产品数量，产品数量 = 原库存数 - 该产品转移的数量
                    warehouseListIn.setProductNumber(warehouseListDtosIn.get(j).getProductNumber() - inventoryTransferListOld.get(i).getInventoryTransferNumber());
                    break; //终止循环
                }
            }
            // 该产品存在，更新库存
            warehouseMapper.updateWarehouseList(warehouseListIn);
            //最后将要删除的子标记录删除
            inventoryTransferMapper.deleteInventoryTransferListById(inventoryTransferListOld.get(i).getInventoryTransferId(), inventoryTransferListOld.get(i).getProduceId());
        }

        return 1;
    }

    /**
     * 更新仓库调拨删除标志位
     *
     * @param inventoryTransfer
     * @return
     */
    @Override
    public int updateInventoryTransferDeleteFlag(InventoryTransfer inventoryTransfer) {
        List<InventoryTransferList> inventoryTransferLists = inventoryTransfer.getInventoryTransferLists(); // 被删除记录的所有子表记录
        List<WarehouseListDto> warehouseListDtosOut = warehouseMapper.selectWarehouseListListById(inventoryTransfer.getWarehouseOutId()); // 查询转出仓库产品列表
        List<WarehouseListDto> warehouseListDtosIn = warehouseMapper.selectWarehouseListListById(inventoryTransfer.getWarehouseInId()); // 查询转入仓库产品列表
        for (int i = 0; i < inventoryTransferLists.size(); i++) {
            // 操作转出仓库库存数量
            WarehouseListDomain warehouseListOut = new WarehouseListDomain(); // 转出仓库子表的对象
            warehouseListOut.setWarehouseId(inventoryTransfer.getWarehouseOutId());
            warehouseListOut.setProduceId(inventoryTransferLists.get(i).getProduceId());
            // 遍历调出仓库
            for (int j = 0; j < warehouseListDtosOut.size(); j++) {
                if (inventoryTransferLists.get(i).getProduceId().equals(warehouseListDtosOut.get(j).getProduceId())) {
                    // 该产品存在，更新产品数量，产品数量 = 原库存数 + 该产品转移的数量
                    warehouseListOut.setProductNumber(warehouseListDtosOut.get(j).getProductNumber() + inventoryTransferLists.get(i).getInventoryTransferNumber());
                    break; //终止循环
                }
            }
            warehouseMapper.updateWarehouseList(warehouseListOut); //更新转出仓库库存数量
            // 操作转入仓库库存数量
            WarehouseListDomain warehouseListIn = new WarehouseListDomain(); // 转入仓库子表的对象
            warehouseListIn.setWarehouseId(inventoryTransfer.getWarehouseInId());
            warehouseListIn.setProduceId(inventoryTransferLists.get(i).getProduceId());
            // 遍历转入仓库
            for (int j = 0; j < warehouseListDtosIn.size(); j++) {
                if (inventoryTransferLists.get(i).getProduceId().equals(warehouseListDtosIn.get(j).getProduceId())) {
                    // 该产品存在，更新产品数量，产品数量 = 原库存数 - 该产品转移的数量
                    warehouseListIn.setProductNumber(warehouseListDtosIn.get(j).getProductNumber() - inventoryTransferLists.get(i).getInventoryTransferNumber());
                    break; //终止循环
                }
            }
            // 该产品存在，更新库存
            warehouseMapper.updateWarehouseList(warehouseListIn);

        }
        return inventoryTransferMapper.updateInventoryTransferDeleteFlag(inventoryTransfer);
    }

    /**
     * 仓库调拨详细查询
     *
     * @param inventoryTransferList
     * @return
     */
    @Override
    public List<InventoryTransferList> selectInventoryTransferListList(InventoryTransferList inventoryTransferList) {
        return inventoryTransferMapper.selectInventoryTransferListList(inventoryTransferList);
    }

    /**
     * 查询仓库调拨对应的所有产品详细
     *
     * @param inventoryTransferId 仓库调拨编号
     * @return
     */
    @Override
    public List<InventoryTransferList> selectInventoryTransferListListById(String inventoryTransferId) {
        return inventoryTransferMapper.selectInventoryTransferListListById(inventoryTransferId);
    }

    /**
     * 查询仓库调拨对应的指定产品详细
     *
     * @param inventoryTransferId
     * @param produceId
     * @return
     */
    @Override
    public InventoryTransferList selectInventoryTransferListById(String inventoryTransferId, String produceId) {
        return inventoryTransferMapper.selectInventoryTransferListById(inventoryTransferId, produceId);
    }
}
